package com.code.exam.controller;

import com.code.exam.annotation.LoginCheck;
import com.code.exam.common.content.BaseContext;
import com.code.exam.common.properties.JwtProperties;
import com.code.exam.common.result.Result;
import com.code.exam.common.utils.JwtUtil;
import com.code.exam.dto.CgPasswordDTO;
import com.code.exam.dto.UserLoginDTO;
import com.code.exam.dto.UserRegisterDTO;
import com.code.exam.entity.User;
import com.code.exam.service.UserService;
import com.code.exam.vo.UserLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


    @Slf4j
    @RestController
    @RequestMapping("/user")
    public class UserController {

        @Autowired
        private UserService userService;
        @Autowired
        private JwtProperties jwtProperties;
        @PostMapping("/login")
        @ApiOperation("学生登入")
        public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
            log.info("学生登录{}", userLoginDTO);
            User user  = userService.login(userLoginDTO);

            //登录成功生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("userId", user.getId());
            claims.put("role", "USER");
            String jwt = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),
                    jwtProperties.getUserTtl(),claims);


            UserLoginVO userLoginVO = UserLoginVO.builder()
                    .id(user.getId())
                    .username(user.getUsername())
                    .token(jwt)
                    .build();

            return Result.success(userLoginVO);
        }



        @PostMapping("/register")
        @ApiOperation("学生注册")
        public Result register(@RequestBody UserRegisterDTO userRegisterDTO){


            log.info("学生注册{}",userRegisterDTO);
            User user = new User();
            BeanUtils.copyProperties(userRegisterDTO,user);
            String username = userService.register(user);
            return Result.success(username);

        }

        @LoginCheck(role = "user")
        @PostMapping("/changepassword")
        @ApiOperation("学生修改密码")
        public Result changepassword(@RequestBody CgPasswordDTO cgPasswordDTO){
            log.info("学生修改密码{}",cgPasswordDTO);
            //校验数据是否为空
            if(cgPasswordDTO.getOldPassword() == null ||
            cgPasswordDTO.getNewPassword() == null)
            {
                return Result.error("旧密码和新密码不能为空");
            }
            Long userId = BaseContext.getCurrentId();
            userService.changepassword(userId,cgPasswordDTO);
            return Result.success("密码修改成功");

        }



}
